<template>
  <div>
    <van-pull-refresh v-model="isRefreshLoading" @refresh="onLoad">
      <MyEmpty v-if="isLoading === false && list.length === 0" />
      <van-list class="list">
        <div class="item" v-for="(item, index) in list" :key="index">
          <div class="info" @click="toJobEdit(item.jobid)">
            <div class="left">
              <b>{{ item.jobName }}</b>
              <p
                v-if="
                  item.isRefresh === true ||
                  item.isTop === true ||
                  item.isAutoZFB === true ||
                  item.isNoOperat === true
                "
              >
                <small class="good" v-if="item.isRefresh === true"
                  >已刷新</small
                >
                <small class="good" v-if="item.isTop === true">排名靠前</small>
                <small class="good" v-if="item.isAutoZFB === true"
                  >自动再发布</small
                >
                <small class="bad" v-if="item.isNoOperat === true"
                  >长期未维护</small
                >
              </p>
              <p>
                <span class="end">{{ item.endDateStr }}</span>
                <i></i>
                <span class="look">{{ item.viewCount }}</span>
              </p>
            </div>
            <div class="right" @click.stop="toInbox(item.jobid)">
              <b>{{ item.getResumeTotal }}</b>
              <span>收到简历</span>
            </div>
          </div>
          <div class="btns">
            <p>
              <span @click="doOperat(item.jobid, 'SRefresh')">刷新</span>
              <span @click="doOperat(item.jobid, 'Stop')">停止</span>
              <span @click="onShare(item)">分享</span>
            </p>
            <p>
              <span class="again" @click="doOperat(item.jobid, 'ZFB')"
                >再发布</span
              >
            </p>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { List, PullRefresh, Dialog, Toast, Sticky } from "vant";
import MyEmpty from "@/components/MyEmpty.vue";
import { CompanyJobGetMyJobs, CompanyJobOperatJobs } from "@/util/http";
import { returnToPhoneWithData } from "@/util/phone";
import Global from "@/Global";
export default {
  name: "JobOnline",
  components: {
    MyEmpty,
    [List.name]: List,
    [PullRefresh.name]: PullRefresh,
    [Sticky.name]: Sticky,
  },
  mounted() {
    this.onLoad();
    this.$bus.$on(Global.bus_company_joblist_update, (res) => {
      if (res === "1") {
        this.onRefresh();
      }
    });
  },
  // 定义属性
  data() {
    return {
      isRefreshLoading: false,
      list: [],
      page: 0,
      isLoading: true,
      finished: false,
    };
  },
  // 方法集合
  methods: {
    onLoad() {
      if (this.isRefreshLoading) {
        this.page = 0;
      }
      let page = this.page;
      page++;
      this.page = page;
      CompanyJobGetMyJobs(page, "1").then((res) => {
        // console.log(res);
        this.isLoading = false;
        if (this.isRefreshLoading) {
          this.list = [];
        }
        this.isRefreshLoading = false;
        let list = res;
        if (list.length > 0) {
          this.list = [...this.list, ...list];
          this.finished = false;
        } else {
          this.finished = true;
        }
      });
    },
    onRefresh() {
      this.isLoading = true;
      this.page = 0;
      this.list = [];
      this.onLoad();
    },
    doOperat(jobIDs, operat) {
      if (jobIDs === "") {
        Toast("请选择职位");
        return;
      }
      if (operat === "ZFB") {
        Dialog.confirm({
          message: `检测到再发布${jobIDs.split(",").length}个职位，将扣除${
            jobIDs.split(",").length
          }个职位数？`,
          confirmButtonText: "再发布",
        })
          .then(() => {
            this.subOperat(jobIDs, operat);
          })
          .catch(() => {
            // on cancel
          });
      } else if (operat === "Stop") {
        Dialog.confirm({
          message: "确认停止这些职位？",
          confirmButtonText: "停止",
        })
          .then(() => {
            this.subOperat(jobIDs, operat);
          })
          .catch(() => {
            // on cancel
          });
      } else {
        this.subOperat(jobIDs, operat);
      }
    },
    subOperat(jobIDs, operat) {
      CompanyJobOperatJobs({ jobIDs, operat }).then((res) => {
        //console.log(res);
        Toast(res.msg);
        if (res.result > 0) {
          this.onRefresh();
          this.$bus.$emit(Global.bus_company_joblist_update, "0");
        }
      });
    },
    toJobEdit(jobid) {
      this.$router.push({ name: "company-job-edit", query: { jobid } });
    },
    toInbox(jobid) {
      this.$router.push({ name: "company-resume-inbox", query: { jobid } });
    },
    onShare(item) {
      console.log(item);
      let title = item.jobName;
      if (item.monthPayVal != null) {
        title += ` ${item.monthPayVal}，强烈推荐`;
      } else {
        title += "，强烈推荐";
      }
      let content = item.jobName;
      returnToPhoneWithData({
        action: "share",
        title,
        content,
        url: Global.share_person_url + `/pages/job/jobInfo?jobid=${item.jobid}`,
      });
    },
  },
};
</script>

<style lang="less">
@import "vant/lib/index.less";
@import "@/assets/css/company.less";
</style>
